package com.ytjj.qmyx.admin.controller.imController;

import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.model.dto.ImServiceGroupDto;
import com.ytjj.qmyx.admin.model.dto.ImServiceUserDto;
import com.ytjj.qmyx.admin.model.vo.ImServiceGroupVO;
import com.ytjj.qmyx.admin.model.vo.ImServiceUserVO;
import com.ytjj.qmyx.admin.service.imService.ImServiceGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/imServiceGroup")
@Slf4j
public class ImServiceGroupController {

    @Autowired
    private ImServiceGroupService imServiceGroupService;


    /**
     * 新增分组
     * 参数: groupName
     * 返回: count
     */
    @PostMapping("/insertGroup")
    public CommonResult insertGroup(@RequestBody ImServiceGroupVO imServiceGroupVO) {
        Integer count = imServiceGroupService.insertGroup(imServiceGroupVO);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 删除分组(逻辑删除)
     * 参数: groupName,isDel
     */
    @PostMapping("/deleteGroup")
    public CommonResult deleteGroup(@RequestBody ImServiceGroupVO imServiceGroupVO) {
        Integer count = imServiceGroupService.deleteGroup(imServiceGroupVO);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 编辑分组
     * 参数: id,groupName
     */
    @PostMapping("/updateGroup")
    public CommonResult updateGroup(@RequestBody ImServiceGroupVO imServiceGroupVO) {
        Integer count = imServiceGroupService.updateGroup(imServiceGroupVO);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 查询分组
     * 参数: "id"-空值
     * 返回: list: [{id,groupName},...]
     */
    @PostMapping("/selectGroup")
    public CommonResult selectGroup(@RequestBody ImServiceGroupVO imServiceGroupVO) {
        List<ImServiceGroupDto> list = imServiceGroupService.selectGroup(imServiceGroupVO);
        return CommonResult.success(list);
    }

    /**
     * 查询所有客服
     * 参数:没有
     * 返回:list:[{id, name, tel, email},...]
     */
    @GetMapping("/selectAllServiceUser")
    public CommonResult selectAllServiceUser() {
        List<ImServiceUserDto> list = imServiceGroupService.selectAllServiceUser();
        return CommonResult.success(list);
    }

    /**
     * 客服分组中添加客服
     * 参数:groupId,userId,maxConNum
     */
    @PostMapping("/addServiceUser")
    public CommonResult addServiceUser(@RequestBody ImServiceUserVO imServiceUserVO) {
        Integer count = imServiceGroupService.addServiceUser(imServiceUserVO);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 通过groupId 查询该组的所有客服
     * 参数: groupId
     * 返回:list:[{id,userId,status,conNum,maxConNum,tel,name,email},...]
     */
    @GetMapping("/selectByGroupId")
    public CommonResult selectByGroupId(@RequestParam Integer id) {
        List<ImServiceUserDto> list = imServiceGroupService.selectByGroupId(id);
        return CommonResult.success(list);
    }

    /**
     * 客服分组里对客服的操作编辑(编辑服务上限)
     */
    @PostMapping("/updateServiceUser")
    public CommonResult updateServiceUser(@RequestBody ImServiceUserVO imServiceUserVO) {
        Integer count = imServiceGroupService.updateServiceUser(imServiceUserVO.getId(), imServiceUserVO.getMaxConNum());
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 客服分组里对客服的操作移除
     */
    @PostMapping("/removeServiceUser")
    public CommonResult removeServiceUser(@RequestBody ImServiceUserVO imServiceUserVO) {
        Integer count = imServiceGroupService.removeServiceUser(imServiceUserVO.getId());
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
